package com.fbinggun.fbinggunutil.gui;

import com.fbinggun.fbinggunutil.API.JavaUtil;
import com.fbinggun.fbinggunutil.API.PApiUtil;
import com.fbinggun.fbinggunutil.gui.item.PgDn;
import com.fbinggun.fbinggunutil.gui.item.PgUp;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * 菜单父类
 */
public abstract class Gui {
    public  Map<String, SlotItem> slotItemMap = new HashMap<>();

    public final ConfigurationSection config;
     boolean cancelPlayerInvCancel=true;

    /**
     * 玩家打开菜单时 是否可操作玩家背包
     * @return
     */
    public boolean isCancelPlayerInvCancel() {
        return cancelPlayerInvCancel;
    }

    /**
     * 设置玩家是否在这个菜单中操作玩家背包
     * @param cancelPlayerInvCancel
     */
    public void setCancelPlayerInvCancel(boolean cancelPlayerInvCancel) {
        this.cancelPlayerInvCancel = cancelPlayerInvCancel;
    }
    PgDn pgDn;
    PgUp pgUp;

    String title;
    int size;
    public final ConfigurationSection itemConfig;

    public Gui(ConfigurationSection config) {
        this.config = config;
        title = config.getString("Title");
        size = config.getInt("Size");
        itemConfig = config.getConfigurationSection("Item");
        if(itemConfig!=null) {
            loadSlotItem();
        }

    }

    /**
     * 获取下一页按钮 可能为空
     * @return
     */
    public PgDn getPgDn() {
        return pgDn;
    }
    /**
     * 获取上一页按钮 可能为空
     * @return
     */
    public PgUp getPgUp() {
        return pgUp;
    }

    /**
     * 获取按钮(物品）配置文件合集
     * @return
     */
    public ConfigurationSection getItemConfig() {
        return itemConfig;
    }

    /**
     * 初始化按钮 （配置文件加载如内存中 一般在构造方法时使用 同启动插件时运行）
     */
    public void loadSlotItem() {
        for (String id : itemConfig.getKeys(false)) {
            String type =itemConfig.getString("Type");
            if(type!=null){
            if(type.equalsIgnoreCase("PgUp")){
                pgUp=new PgUp("PgUp",itemConfig.getConfigurationSection(id));
                continue;
            }
            if(type.equalsIgnoreCase("PgDn")){
                pgDn =new PgDn("PgDn",itemConfig.getConfigurationSection(id));
                continue;
            }}
            slotItemMap.put(id, new SlotItem(id, itemConfig.getConfigurationSection(id)));
        }
    }

    /**
     * 获取菜单标题
     * @param player
     * @return
     */
    public String getTitle(Player player) {
        if(title!=null){
            String msg;
            if(player!=null){
                msg = PApiUtil.setPlaceholders(player, title);
            }else {
                msg = JavaUtil.replaceColor(title);
            }
            return msg;
        }
        return null;
    }
    /**
     * 获取菜单标题
     * @return
     */

    public String getTitle() {
        return getTitle(null );
    }

    /**
     * 获取菜单大小
     * @return
     */
    public int getSize() {
        return size;
    }


    /**
     * 为玩家打开菜单
     * @param player
     */
    public void openInventory(Player player) {
        openInventory(player, 1);
    }

    /**
     * 为玩家打开菜单
     * @param player
     * @param page 页数 如不需要请填1
     */
    public abstract void openInventory(Player player, int page);

    public Map<String, SlotItem> getSlotItemMap() {
        return slotItemMap;
    }

    /**
     * 获取已加载如内存中的按钮
     * @param player
     * @return
     */
    public Map<String, SlotItem> getSlotItemMap(Player player) {
        return slotItemMap;
    }

    /**
     * 这个按钮物品 是否是翻页按钮
     * @param item
     * @return
     */
    public boolean isPage(SlotItem item){
        if(item instanceof PgDn){
            return true;
        }
        if(item instanceof PgDn){
            return true;
        }
        return false;
    }

    /**
     * 玩家是否有权打开菜单
     * @param player
     * @return
     */
    public abstract boolean isOpen(Player player);

    /**
     * 点击菜单的操作
     * @param inventoryClickEvent
     * @param fHolder
     */
    public abstract void run(InventoryClickEvent inventoryClickEvent,FHolder fHolder);
}
